﻿namespace LogManager.Utils.IO
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class PathHelper
    {
        public static void CheckFullAccessDirectory(string path)
        {
            Directory.GetDirectories(path);
            Directory.GetFiles(path);
            CreateDeleteRandomFile(path);
        }

        private static void CreateDeleteRandomFile(string path)
        {
            var fileName = path + "\\TestFile_" + Path.GetRandomFileName();

            //création / suppression
            File.Create(fileName).Close();
            File.Delete(fileName);
        }

        public static List<FileInfo> GetFilesOlderThan(string directoryPath, TimeSpan maxOld)
        {
            var dirInfo = new DirectoryInfo(directoryPath);
            var allFiles = dirInfo.GetFiles("*", SearchOption.AllDirectories); ;

            var nowutc = DateTime.UtcNow;
            return allFiles.Where(p => nowutc - p.LastWriteTimeUtc > maxOld).ToList();
        }
    }
}
